package com.abigdreamer.im.client.module.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.abigdreamer.im.client.module.main.headtree.TreeScrollPane;
import com.abigdreamer.im.client.module.main.search.AutoCompletionField;
import com.abigdreamer.im.client.module.main.search.UserFilter;
import com.abigdreamer.im.client.module.main.weather.WeatherLabel;
import com.abigdreamer.im.client.module.vo.Cache;
import com.abigdreamer.im.client.module.vo.MsgFriendGroup;
import com.abigdreamer.im.client.module.vo.MsgUser;
import com.abigdreamer.im.client.resourse.ResourcesManagement;
import com.abigdreamer.im.client.ui.trayIcon.EIMTrayIcon;
import com.abigdreamer.im.client.util.ClientIMUtil;
import com.abigdreamer.im.client.util.EIMClientConfig;
import com.abigdreamer.im.client.util.EIMLogger;
import com.abigdreamer.swing.EmptyComponent;
import com.abigdreamer.swing.app.SkinableWindow;
import com.abigdreamer.swing.app.SwingUtil;
import com.abigdreamer.swing.component.JCLabel;
import com.abigdreamer.util.ImageUtil;

import craky.componentc.JCComboBox;
import craky.layout.LineLayout;

public class EIMMainFrame extends SkinableWindow {

	private static final long serialVersionUID = -2699292233455801786L;
	
	private AutoCompletionField findField;
	private javax.swing.JPanel friendPanel;
	private TreeScrollPane friendTree;
	private JCLabel headImage;
	private SnapTipTabbedPane mainTabbedPanel;
	private JCLabel personWord;
	private JCComboBox stateComboBox;
	private RButton sysSetButton;
	private JPanel teamPanel;
	private JPanel zuijinPanel;

	private static List<MsgFriendGroup> friends = new ArrayList<MsgFriendGroup>();
	private static MsgUser ower = new MsgUser();

	private static EIMMainFrame instance;

	public static EIMMainFrame getInstance() {
		if (instance == null) {
			instance = new EIMMainFrame();
		}
		return instance;
	}

	private EIMMainFrame() {
		initComponents();
		
		skinMenuVisible = true;
	}
	
	@Override
	protected void onWindowClosing() {
		System.exit(0);
	}

	public void changTryIcon() {
		EIMTrayIcon qqTrayIcon = EIMTrayIcon.getInStance();
		qqTrayIcon.setFrame(this);
		qqTrayIcon.setTrayIconState(EIMClientConfig.OnlineTryIcon_Type);
		qqTrayIcon.showIcon(EIMClientConfig.OnlineTryIcon_Type);
	}
	
	private void initComponents() {
		this.setSize(287, 722);
		
		setTitle("ABD-IM 2012");
		
		setMinimumSize(new Dimension(287, 400));

		headImage = new JCLabel();
		stateComboBox = ClientIMUtil.createIMStateCombox();
		

		sysSetButton = new RButton(RButton.RIGHT_ROUND_RECT);

		mainTabbedPanel = new SnapTipTabbedPane();
		friendPanel = new JPanel();
		friendTree = new TreeScrollPane(friends);
		teamPanel = new JPanel();
		zuijinPanel = new JPanel();

		String headnum = (String) ower.getAttribute("headImage");
		BufferedImage image = ResourcesManagement.getBufferedImage("head/" + headnum + ".gif");
		headImage.setIcon(new ImageIcon(ImageUtil.makeRoundedCorner(image, 8)));
		
		EmptyComponent ecHeadLabel = new EmptyComponent();
		ecHeadLabel.setLayout(new LineLayout(10, 8, 0, 5, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		ecHeadLabel.setPreferredSize(new Dimension(-1, 25));
		ecHeadLabel.add(stateComboBox, LineLayout.START);
		ecHeadLabel.add(new JCLabel(ower.getUserName()), LineLayout.START);
		
		EmptyComponent ecHeadMessage = new EmptyComponent();
		ecHeadMessage.setLayout(new GridLayout(2, 1));
		ecHeadMessage.setPreferredSize(new Dimension(-1, 55));
		ecHeadMessage.add(ecHeadLabel);
		
		personWord = new JCLabel("   " + ower.getPersonWord());
		ecHeadMessage.add(personWord);
		
		EmptyComponent ecUser = new EmptyComponent();
		ecUser.setLayout(new LineLayout(0, 8, 0, 0, 5, LineLayout.CENTER, LineLayout.LEADING, LineLayout.HORIZONTAL));
		ecUser.setPreferredSize(new Dimension(-1, 60));
		ecUser.add(headImage, LineLayout.START);
		ecUser.add(ecHeadMessage, LineLayout.MIDDLE);
		
		ecUser.add(new WeatherLabel(), LineLayout.END);

		EIMLogger.INFO("成功添加weather面板...");

		JPanel searchPanel = new JPanel(); // 创建面板
		searchPanel.setLayout(null);
		searchPanel.setOpaque(false);
		
		findField = new AutoCompletionField();
		findField.setFilter(new UserFilter(Cache.getInstance().getfriendMap()));
		findField.setBorder(BorderFactory.createEmptyBorder());
		findField.setBounds(8, 2, 270, 24); // 设置控件坐标大小
		searchPanel.add(findField);

		BufferedImage img = null;
		try {
			img = ImageUtil.cut9Image(SwingUtil.getBufferedImage("Themes/Default/MainPanel/search/main_search_bkg.png"), BufferedImage.TYPE_INT_ARGB, 10, 10, 278, 28);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JLabel searchBg = new JLabel();
		searchBg.setIcon(new ImageIcon(img));
		searchBg.setBounds(2, 0, 282, 28);

		searchPanel.add(searchBg);
		searchPanel.setPreferredSize(new Dimension(-1, 28));

		EmptyComponent ecNorth = new EmptyComponent();
		ecNorth.setLayout(new LineLayout(0, 0, 0, 0, 0, LineLayout.CENTER, LineLayout.LEADING, LineLayout.VERTICAL));
		ecNorth.setPreferredSize(new Dimension(-1, 100));
		ecNorth.add(ecUser, LineLayout.START_FILL);
		ecNorth.add(searchPanel, LineLayout.MIDDLE_FILL);

		getContentPane().add(ecNorth, BorderLayout.NORTH);

		mainTabbedPanel.addTab("好友列表", friendTree);
		mainTabbedPanel.addTab("群列表", teamPanel);
		mainTabbedPanel.addTab("最近联系人", zuijinPanel);
		getContentPane().add(mainTabbedPanel, BorderLayout.CENTER);
		getContentPane().add(sysSetButton, BorderLayout.SOUTH);
	}

	public void initMainTree(List<MsgFriendGroup> groupList) {
		TreeScrollPane treeScrollPane = new TreeScrollPane(groupList);
		friendTree = treeScrollPane;
		this.repaint();
	}

	public void setMainColor(Color bg) {
		mainTabbedPanel.setBackground(bg);
		friendPanel.setBackground(bg);
		teamPanel.setBackground(bg);
		zuijinPanel.setBackground(bg);
	}

	public static List<MsgFriendGroup> getFriends() {
		return friends;
	}

	public static void setFriends(List<MsgFriendGroup> friends) {
		EIMMainFrame.friends = friends;
	}

	public static MsgUser getOwer() {
		return ower;
	}

	public static void setOwer(MsgUser ower) {
		EIMMainFrame.ower = ower;
	}

	public TreeScrollPane getFriendTree() {
		return friendTree;
	}

	public void setFriendTree(TreeScrollPane friendTree) {
		this.friendTree = friendTree;
	}

}